import router from "../index";
import { constantRouterMap,notFoundRouter } from './staticRouter'
import userStore from "@/stores/modules/user"
import { getFlatArr } from '@/utils/util'
import permissionStore from "@/stores/modules/permission"
// 引入 views 文件夹下所有 vue 文件
const modules = import.meta.glob("@/views/**/*.vue");

/**
 * 初始化动态路由
 */
export const initDynamicRouter = async () => {
	try {
        const user = userStore()
        const per = permissionStore()
        await per.getAuthMenuList({token:user.token});
		// 添加动态路由 (getFlatArr 方法把菜单全部处理成一维数组，方便添加动态路由)
		let dynamicRouter = getFlatArr(JSON.parse(JSON.stringify(per.authMenuListGet)));
		dynamicRouter.forEach((item) => {
			if (item.children)
				delete item.children;
			// if (item.path){
            //     item.component = modules["/src/views" + item.path + ".vue"];
            // }
			// 	router.addRoute(item);
		});
		console.log(router.options,'router');

		// 添加 notFoundRouter
		router.addRoute(notFoundRouter);
	} catch (error) {
		router.replace(constantRouterMap);
		return Promise.reject(error);
	}
};